//
//  AY_DetailIntroCell.m
//  AiYue
//
//  Created by mac on 2023/10/24.
//

#import "AY_DetailIntroCell.h"
#import "AY_DirectoryPage.h"
#import "STPopupController.h"
@interface AY_DetailIntroCell ()<SXDirectoryPageDelegate>

@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UILabel *introLabel;
@property (nonatomic, strong) UILabel *intro;
@property (nonatomic, strong) UILabel *lastChapterName;
@property (nonatomic, strong) UILabel *lastChapter;
@property (nonatomic, strong) UILabel *lastChapterTime;
@property (nonatomic, strong) STPopupController * popVericodeController;
@property (nonatomic, strong) UIView *line1;

@end

@implementation AY_DetailIntroCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        self.backgroundColor = COLOR(@"#F6F7FB");
        self.backView = [AYTool createViewWithFrame:CGRectZero andBg:[UIColor whiteColor] andRadius:YNUM(13) andAddSubView:self.contentView];
        [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(YNUM(12));
            make.top.equalTo(self.contentView).offset(YNUM(12));
            make.right.equalTo(self.contentView).offset(-YNUM(12));
            make.bottom.equalTo(self.contentView);
        }];
        
        self.introLabel = [AYTool createLabelWithFrame:CGRectZero andText:@"目录" andTextColor:COLOR(@"#000002") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(18)] andLines:1 andRadius:0 andAddSubView:self.backView];
        [self.introLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self.backView).offset(YNUM(16));
            make.height.mas_offset(YNUM(24));
        }];
        
        UIImageView * arrowImg = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"icon_arrow_detail.png") andBg:[UIColor clearColor] andRadius:YNUM(4) andAddSubView:self.backView];
        [arrowImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.backView).offset(YNUM(-16));
            make.height.mas_equalTo(YNUM(16));
            make.centerY.equalTo(self.introLabel);
            make.width.mas_equalTo(YNUM(16));
        }];
                
        self.lastChapterTime = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#97A4B0") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(12)] andLines:1 andRadius:0 andAddSubView:self.backView];
        [self.lastChapterTime mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(arrowImg.mas_left).offset(YNUM(-8));
            make.centerY.equalTo(self.introLabel);
        }];
        
        self.line1 = [AYTool createViewWithFrame:CGRectZero andBg:COLOR(@"f6f6f6") andRadius:0 andAddSubView:self.backView];
        [self.line1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.introLabel);
            make.right.equalTo(self.backView).offset(-YNUM(16));
            make.height.mas_equalTo(1);
            make.top.equalTo(self.introLabel.mas_bottom).offset(YNUM(15));
        }];
        
        UIButton * muluBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:nil andFont:nil andTitleColor:[UIColor clearColor] andBg:nil andImage:nil andBgImage:nil andTarget:self andAction:@selector(handleMuluAction) andRadius:YNUM(8) andAddSubView:self.backView];
        [muluBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.top.right.equalTo(self.backView);
        }];
        
        
        self.lastChapterName = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#000002") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(18)] andLines:1 andRadius:0 andAddSubView:self.contentView];
        self.lastChapterName.textAlignment = NSTextAlignmentLeft;
        [self.lastChapterName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.backView).offset(YNUM(14));
            make.right.equalTo(self.backView).offset(-YNUM(16));
            make.top.equalTo(self.line1.mas_bottom).offset(YNUM(16));
        }];
        
        self.lastChapter = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#000002") andBgColor:nil andFont:YFont(15) andLines:7 andRadius:0 andAddSubView:self.backView];
        [self.lastChapter mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.introLabel);
            make.right.equalTo(self.backView).offset(-YNUM(16));
            make.top.equalTo(self.lastChapterName.mas_bottom).offset(YNUM(15));
        }];
    }
    return self;
}

#pragma mark - 点击目录
- (void)didSelectMuluWithChapterIndex:(NSInteger)index {
    [self.popVericodeController dismiss];
    AY_PageReadViewController *vc = [[AY_PageReadViewController alloc] init];
    [vc sendBookId:self.bookModel.bookId andPage:@"0" andChapterIndex:[NSString stringWithFormat:@"%ld",index]];
    [[AYTool viewControllerWith:self].navigationController pushViewController:vc animated:YES];
}

- (void)setBookModel:(BookModel *)bookModel {
    _bookModel = bookModel;
    
    if ([bookModel.status isEqual:@"2"]) {
        self.lastChapter.hidden = YES;
        self.lastChapterName.hidden = YES;
        self.line1.hidden = YES;
        self.lastChapterTime.text = [NSString stringWithFormat:@"共%@章",bookModel.chapterNum];
    }else {
        self.line1.hidden = NO;
        self.lastChapter.hidden = NO;
        self.lastChapterName.hidden = NO;
        if (!kStringIsEmpty(bookModel.lastChapter.content)) {
            self.lastChapter.attributedText = [AYTool LineSpacingTextWithText:bookModel.lastChapter.content andSpace:YNUM(8)];
        }
        if (!kStringIsEmpty(bookModel.lastChapter.name)) {
            self.lastChapterName.text = bookModel.lastChapter.name;
        }
        if (!kStringIsEmpty(bookModel.lastChapter.gmtModified)) {
            NSString *time = [bookModel.lastChapter.gmtModified stringByReplacingOccurrencesOfString:@"T" withString:@" "];
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            //需要设置为和字符串相同的格式
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            NSDate *localDate = [dateFormatter dateFromString:time];
            
            NSDate *currentDate = [NSDate date];
            NSString *currentDateString = [dateFormatter stringFromDate:currentDate];
            NSDate *localDate1 = [dateFormatter dateFromString:currentDateString];
            [self compare:localDate to:localDate1];
        }
    }
}

-(void)compare:(NSDate *)startTime to:(NSDate *)endTime{
    // 当前日历
    NSCalendar *calendar = [NSCalendar currentCalendar];
    // 需要对比的时间数据
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth
    | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    // 对比时间差
    NSDateComponents *dateCom = [calendar components:unit fromDate:startTime toDate:endTime options:0];
    NSString *time = [NSString stringWithFormat:@"%ld%ld%ld%ld%ld%ld",dateCom.year,dateCom.month,dateCom.day,dateCom.hour,dateCom.minute,dateCom.second];
    if (dateCom.year > 0 || dateCom.month > 0 || dateCom.day > 7) {
        self.lastChapterTime.text = [NSString stringWithFormat:@"最新第%@章",self.bookModel.chapterNum];
    }else {
        if (dateCom.day >= 1  && dateCom.day <= 7) {
            self.lastChapterTime.text = [NSString stringWithFormat:@"%ld天前更新第%@章",(long)dateCom.day,self.bookModel.chapterNum];
        }else {
            if (dateCom.hour < 1) {
                self.lastChapterTime.text = [NSString stringWithFormat:@"1小时前更新第%@章",self.bookModel.chapterNum];
            }else {
                self.lastChapterTime.text = [NSString stringWithFormat:@"%ld小时前更新第%@章",(long)dateCom.hour,self.bookModel.chapterNum];
            }
        }
    }
}

- (void)handleMuluAction {
    if(kStringIsEmpty(self.bookModel.bookId)) {
        ShowToast(@"暂无目录");
        return;
    }
    [AYApiRequest getBookChapterListWithPageNum:@"1" andPageSize:@"10000" andArticleId:self.bookModel.bookId Success:^(AY_ChapterModel * _Nonnull pkg) {
        if([pkg.code isEqualToString:@"200"]) {
            [self navTopDirectoryPage:pkg.rows];
        }
    } failure:^(int status, NSString * _Nonnull info) {
        ShowToast(@"亲，网络不顺畅哟~");
    }];
}

- (void)navTopDirectoryPage:(NSArray *)dataAry {
    AY_DirectoryPage * page = [[AY_DirectoryPage alloc] initDirectoryPageDataSource:dataAry];
    page.delegate = self;
    self.popVericodeController = [[STPopupController alloc] initWithRootViewController:page];
    self.popVericodeController.style = STPopupStyleBottomSheet;
    [self.popVericodeController presentInViewController:[AYTool viewControllerWith:self]];
}

@end
